
#include <butil/logging.h>
#include <brpc/server.h>
#include <brpc/channel.h>
#include <json2pb/pb_to_json.h>
#include <algorithm>
#include <cstring>
#include <functional>
#include <string>
#include <vector>

#include <thread>
#include <condition_variable>

#include <butil/logging.h>
#include "dmatest.pb.h"
#include "doca_brpc_unit.h"

DEFINE_string(attachment, "", "Carry this along with requests");
DECLARE_string(protocol);
DECLARE_string(connection_type);
DECLARE_int32(timeout_ms);
DEFINE_int32(port, 31010, "server brpc port");
// DEFINE_int32(port, 31111, "server brpc port");
DEFINE_string(listen_addr, "",
              "Server listen address, may be IPV4/IPV6/UDS."
              " If this is set, the flag port will be ignored");
// DEFINE_string(listen_addr, "localhost:31111",
//               "Server listen address, may be IPV4/IPV6/UDS."
//               " If this is set, the flag port will be ignored");
DEFINE_int32(brpc_thread_num, 16, "server brpc thread_num");
DEFINE_string(brpc_service_name, "grpc_mock_dpu_server", "service name");
DEFINE_int32(idle_timeout_s, -1,
             "Connection will be closed if there is no "
             "read/write operations during the last `idle_timeout_s'");
DECLARE_string(load_balancer);

namespace rap {

class DocaDmaServiceImpl final : public DocaDmaService { //CPU侧进入这里
public:
    DocaDmaServiceImpl() {}
    ~DocaDmaServiceImpl() {}
    void InitDmaChannel(::google::protobuf::RpcController *controller, const ::rap::DmaUnitInit *request,
                    ::rap::DmaUnitResponse *response, ::google::protobuf::Closure *done) override {
        (void) controller;
        brpc::ClosureGuard done_guard(done);

        BrpcUnit & brpcunit = BrpcUnit::GetInstance();
        brpcunit.InitDma(request, response);

        return;
    }

    void TaskDispatch(::google::protobuf::RpcController *controller, const ::rap::TaskSend *request,
                    ::rap::TaskFinish *response, ::google::protobuf::Closure *done) override {
        (void) controller;
        (void) request;
        (void) response;
        brpc::ClosureGuard done_guard(done);

        response->set_finishflag(1);
        return;
    }
};
}

int main(int argc, char **argv) {

    // Parse gflags. We recommend you to use gflags as well.
    // GFLAGS_NS::ParseCommandLineFlags(&argc, &argv, true);
    gflags::ParseCommandLineFlags(&argc, &argv, true);

    // Generally you only need one Server.
    brpc::Server server;

    rap::DocaDmaServiceImpl service_impl;

    xmh::Reporter::StartReportThread();
    
    if (server.AddService(&service_impl, brpc::SERVER_DOESNT_OWN_SERVICE) != 0) {
        LOG(ERROR) << "Fail to add service";
        return -1;
    }
    butil::EndPoint point;
    if (!FLAGS_listen_addr.empty()) {
        // if (butil::str2endpoint(FLAGS_listen_addr.c_str(), &point) < 0) {
        if (butil::str2endpoint("127.0.0.1:31111", &point) < 0) {
            LOG(ERROR) << "Invalid listen address:" << FLAGS_listen_addr;
            return -1;
        }
    } else {
        point = butil::EndPoint(butil::IP_ANY, FLAGS_port);
    }
    // Start the server.
    brpc::ServerOptions options;
    options.idle_timeout_sec = FLAGS_idle_timeout_s;
    if (server.Start(point, &options) != 0) {
        LOG(ERROR) << "Fail to start EchoServer";
        return -1;
    }

    // Wait until Ctrl-C is pressed, then Stop() and Join() the server.
    server.RunUntilAskedToQuit();

    xmh::Reporter::StopReportThread();
    xmh::Reporter();

    return 0;
}